using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UI 遮罩管理器
/// 功能：负责弹出窗体模态显示，实现禁止穿透的效果
/// </summary>
public class UIMaskMgr : MonoBehaviour
{
    
    /*字段*/
    //本脚本私有 单例
    private static UIMaskMgr instance = null;
    //UI根节点对象
    private GameObject _GoCanvasRoot = null;
    //UI脚本节点对象
    private Transform _TraUIScriptsNode = null;
    //顶层面板
    private GameObject _GoTopPanel;
    //遮罩面板
    private GameObject _GoMaskPanel;
    //UI摄像机
    private Camera _UICamera;
    //UI摄像机层深
    private float _OriginalUICameraDepth;
    
    //得到实例
    public static UIMaskMgr Instance()
    {
        if(instance == null) 
        { 
            instance = new GameObject("_UIMaskMgr").AddComponent<UIMaskMgr>();
        }
        return instance;
    }

    void Awake()
    {
        //初始化字段
        //得到UI根节点对象，脚本节点对象
        _GoCanvasRoot = GameObject.FindGameObjectWithTag(SysDefine.SYS_TAG_CANVAS);
        _TraUIScriptsNode = UnityHelper.FindTheChildNode(_GoCanvasRoot, SysDefine.SYS_SCRIPTMANAGER_NODE);
        //把本脚本实例作为脚本节点的子节点
        UnityHelper.AddChildNodeToParentNode(_TraUIScriptsNode,gameObject.transform);
        //得到顶层面板、遮罩面板
        _GoTopPanel = _GoCanvasRoot;
        _GoMaskPanel = UnityHelper.FindTheChildNode(_GoCanvasRoot, "_UIMaskPanel").gameObject;
        //得到UI摄像机原始层深，在遮罩时该摄像机必须最大，关掉遮罩时恢复原始值。
        _UICamera  = GameObject.FindGameObjectWithTag("_TagUICamera").GetComponent<Camera>();
        if (_UICamera != null)
        {
            //得到原始层深
            _OriginalUICameraDepth = _UICamera.depth;
        }
        else
        {
            Debug.Log("UICamera 为空 检测其Tag或存在情况");
        }
    }
    /// <summary>
    /// 设置遮罩状态
    /// </summary>
    /// <param name="goDisplayUIForms">显示需要的UI窗体</param>
    /// <param name="lucencyType">显示透明度属性</param>
    
    public void SetMaskWindow(GameObject goDisplayUIForms, UIFormLucencyType lucencyType = UIFormLucencyType.Lucency)
    {
        //顶层窗体下移
        _GoTopPanel.transform.SetAsLastSibling();
        //启用遮罩窗体以及设置透明度
        
        _GoMaskPanel.SetActive(true);
        switch (lucencyType)
        {
            case UIFormLucencyType.Lucency: //完全透明，不能穿透
                _GoMaskPanel.GetComponent<Image>().color = SysDefine.SYS_COLOR_LUCENCY;
                break;
            case UIFormLucencyType.Translucence://半透明，不能穿透
                _GoMaskPanel.GetComponent<Image>().color = SysDefine.SYS_COLOR_TRANSLUCENCE;
                break;
            case UIFormLucencyType.ImPenetrable://低透明，不能穿透
                _GoMaskPanel.GetComponent<Image>().color = SysDefine.SYS_COLOR_IMPENETRABLE;
                break;
            case UIFormLucencyType.Penetration: //可以穿透
                if (_GoMaskPanel.activeInHierarchy)
                {
                    _GoMaskPanel.gameObject.SetActive(false); 
                }
                break;
            default:
                break;
        }
        
        
        
        //遮罩窗体下移
        _GoMaskPanel.transform.SetAsLastSibling();
        //显示窗体的下移
        goDisplayUIForms.transform.SetAsLastSibling();
        //增加当前UI摄像机层深，保证其为最前显示
        if (_UICamera!=null)
        {
            _UICamera.depth = _UICamera.depth + 100;
        }
    }
    
    //取消遮罩状态
    public void CancelMaskWindow()
    {
        //顶层窗体下移
        _GoTopPanel.transform.SetAsFirstSibling();
        //禁用遮罩窗体
        if (_GoMaskPanel.activeInHierarchy)
        {
            _GoMaskPanel.SetActive(false);
        }
        
        //恢复层深
        if (_UICamera!=null)
        {
            _UICamera.depth = _OriginalUICameraDepth;
        }
    }
}
